<?php
require('config.php');
// utility functions
function directoryToArray($directory, $recursive) {
	$array_items = array();
	if ($handle = opendir($directory)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				if (is_dir($directory. "/" . $file)) {
					if($recursive) {
						$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
					}
					$file = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $file);
				} else {
					$file = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $file);
				}
			}
		}
		closedir($handle);
	}
	return $array_items;
}



function show_tag($item_id,$tag_id,$tag) {
	$tag_parts=explode('::',$tag);
	$str="<span class=\"tag\" id=\"tag_id$tag_id\">" . $tag_parts[0] . ': ' . 
	"<a href=\"list_items_by_tag.php?q=$tag_id\">" .$tag_parts[1] . "</a>";
	if ($item_id>0) {
		$str.="<a href=\"delete_tag.php?item_id=$item_id&tag_id=$tag_id\" class=\"remove_tag\" title=\"remove tag\">x</a>";
	}
	$str.="</span>";
	echo $str;
}


function show_all_tags($item_id,$dbfilename) {
// list all tags 	
	$db=new SQLite3($dbfilename);
	$query="SELECT t.* FROM tag_t t, map_t m WHERE (m.item_id='$item_id' AND t.tag_id=m.tag_id) GROUP BY t.tag_id;";
	$results=$db->query($query);

	while($res = $results->fetchArray(SQLITE3_ASSOC)){
		$tag = $res['value'];
		$tag_id = $res['tag_id'];
		show_tag($item_id,$tag_id,$tag); 
	}
	
	
	$db->close();
	
}

function show_item($item_id,$dbfilename,$full)
{
	echo $item_id . "<br>";
	show_all_tags($item_id,$dbfilename);
}


